package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.project.sys.entity.OrgPerson;
import com.ctcemti.mdm.project.sys.service.IOrgPersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;

@RestController
@Api(value = "LoginController", description = "登录控制器类")
@RequestMapping("login")
public class LoginController {
    @Autowired
    private IOrgPersonService orgPersonService;

    @ApiOperation(value = "登录系统")
    @PostMapping("loginSystem")
    @ApiImplicitParam(name="person",value = "登录实体类")
    public Result loginSystem(HttpServletRequest request, OrgPerson person) throws NoSuchAlgorithmException {
        return orgPersonService.loginSystem(request,person);
    }


    /* *
     * @Author bxf
     * @Description
     * @Date 11:35 2019/9/10
     * @param request
     * @param person
     * @param flag 更新验证标记（1位验证2为更新）
     * @return com.ctcemti.mdm.common.result.Result
     */
    @ApiOperation(value = "修改密码")
    @PostMapping("changePassWord")
    @ApiImplicitParam(name="person",value = "实体类")
    public Result changePassWord(HttpServletRequest request, OrgPerson person,Integer flag) throws NoSuchAlgorithmException {
        int count = orgPersonService.changePassWord(person,flag);
        return new Result(ResultEnum.SUCCESS,count);
    }

    /* *
     * @Author bxf
     * @Description
     * @Date 11:35 2019/9/10
     * @param request
     * @param person
     * @param flag 更新验证标记（1位验证2为更新）
     * @return com.ctcemti.mdm.common.result.Result
     */
    @ApiOperation(value = "获取用户名和用户id")
    @PostMapping("getPerson")
    public Result getPerson(HttpServletRequest request) throws NoSuchAlgorithmException {
        OrgPerson person = new OrgPerson();
        String token = request.getHeader("Authorization");
        person.setLoginName(JWTUtil.getLoginName(token));
        person.setId(JWTUtil.getLoginId(token));
        return new Result(ResultEnum.SUCCESS,person);
    }


}
